package com.yida.service;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;import org.apache.ibatis.javassist.expr.NewArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.yida.bean.photo;
import com.yida.bean.phototype;
import com.yida.dao.Mapper;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;


@Service
public class serverphotoImpl implements serverphoto{

	@Autowired
	public  Mapper mapper;
	
	//相册的分类
	@Override
	public List<phototype> phototype() {
		// TODO Auto-generated method stu
		return mapper.selectphototype();
	}

	//相册下面的照片
	@Override
	public List<photo> photos(Integer id) {
		// TODO Auto-generated method stub
		return mapper.phototype(id);
	}

	//根据id查分类
	@Override
	public com.yida.bean.phototype phototypebyid(Integer id) {
		// TODO Auto-generated method stub
		return mapper.selectphototypebyid(id);
	}

	
	//删除相册
	@Transactional
	@Override
	public boolean phototypedelete(Integer id) {
		// TODO Auto-generated method stub
		boolean wenjian = false;
		boolean wenjianjia = false;
		boolean shujuku = false;
		String filehead = System.getProperties().getProperty("user.home");
		
//删除文件		
		List<photo> photos = mapper.phototype(id);
		
		if (photos!=null) {
			for (int i = 0; i < photos.size(); i++) {
				String url = filehead+photos.get(i).getImgurl();
				wenjian =	new File(url).delete();
				
			}
		}
		
		
//查到分类分件
	    phototype phototype =	this.phototypebyid(id);
	    
	    HanyuPinyinOutputFormat  format = new HanyuPinyinOutputFormat();
	    format.setToneType(HanyuPinyinToneType.WITHOUT_TONE); 
	    format.setVCharType(HanyuPinyinVCharType.WITH_V);
	    String pinyin = null;
	    try {
			 pinyin =  PinyinHelper.toHanYuPinyinString(phototype.getName(), format ,"" , true);
		} catch (BadHanyuPinyinOutputFormatCombination e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    
//删除文件夹
	    String url2 = filehead+"/blog/imgphoto/"+pinyin;
	   
	    wenjianjia =   new File(url2).delete();
	  
	    
	      if (mapper.phototypedelete(id)>0) {
			shujuku = true;
		}
	    
	    if (wenjian && wenjianjia && shujuku) {
	    	return true;
		}else {
			return false;
		}
	    
		
	}

	
	//添加相册
	@Override
	public Integer insertphototype(phototype phototype) {
		// TODO Auto-generated method stub
		
		
		return mapper.insertphototype(phototype);
	}

	//添加图片
	@Override
	public Integer insertphoto(photo photo) {
		// TODO Auto-generated method stub
		
		
		return mapper.insertphoto(photo);
	}

	//更改相册
	@Override
	public Integer phototypeupdate(com.yida.bean.phototype phototype) {
		// TODO Auto-generated method stub
		
		return mapper.phototypeupdate(phototype);
	}

	@Transactional
	//删除图片
	@Override
	public boolean photodelete(Integer id,String imgurl) {
		// TODO Auto-generated method stub
		Integer shujuku = mapper.photodelete(id);
		boolean result = false;
		
		String filehead = System.getProperties().getProperty("user.home");
		File file = new File(filehead+imgurl);
		
		if (file.exists()) {
			 result =  file.delete();
		}
		
		if (shujuku>0 && result) {
			return true;
		}else {
			return false;
		}
		
		
	}

	@Transactional
	@Override
	public boolean insertphotoup(Integer id,String name,MultipartFile files[]) {
		// TODO Auto-generated method stub
		String userhome = System.getProperties().getProperty("user.home");
		String photohead = File.separator+"blog"+File.separator+"imgphoto"+File.separator;
		
		HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
		 format.setToneType(HanyuPinyinToneType.WITHOUT_TONE); 
		 format.setVCharType(HanyuPinyinVCharType.WITH_V);
		 String pinyin = null;
		try {
			pinyin = PinyinHelper.toHanYuPinyinString(name, format ,"" , true);
		} catch (BadHanyuPinyinOutputFormatCombination e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		for (int i = 0; i < files.length; i++) {
			//获取后窜
			String oldname = files[i].getOriginalFilename();
			String suffix = oldname.substring(oldname.lastIndexOf("."));
			//获取时间数据
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd_HH_mm_ss");
			String filenme  =  dateFormat.format(new Date());
			
			//创建对象
			photo photo = new photo();
			photo.setImgurl(photohead+pinyin+File.separator+filenme+"-"+i+suffix);
			photo.setType(id);
			
			try {
				//插入数据库
				mapper.insertphoto(photo);
				//移入文件
				files[i].transferTo(new File(userhome+photohead+pinyin+File.separator+filenme+"-"+i+suffix));				
			} catch (Exception e) {
				// TODO: handle exception
				return false;
			}
			
		
		}
		
		return true;
		
	}

	
	
}
